from Home.models import *
# NavigationModel,CategoryModel,CourseModel,ChannelGoodsMdoel,ChannelModel,ChannelCatesModel

from rest_framework import serializers

class NavigationSerilize(serializers.ModelSerializer):
    class Meta():
        model=NavigationModel
        fields="__all__"

class SonSerialize(serializers.ModelSerializer):
    class Meta():
        model=CategoryModel
        fields=('id','name')

class CategorySerilize(serializers.ModelSerializer):
    son=SonSerialize(many=True,source="categorymodel_set")

    class Meta():
        model=CategoryModel
        fields = ('id', 'name',"son")

class TeacherSer(serializers.ModelSerializer):
    class Meta:
        model = TeacherModel
        fields="__all__"

class CourseSerilize(serializers.ModelSerializer):
    pid=serializers.SlugRelatedField("name",read_only=True)
    level = serializers.SerializerMethodField()
    teacherd=TeacherSer(source="teacher")

    def get_level(self,obj):
        return obj.get_level_display()
    class Meta():
        model=CourseModel
        fields = "__all__"

class ChannelSer(serializers.ModelSerializer):
    class Meta:
        model = ChannelModel
        fields="__all__"

class ChannelCategorySer(serializers.ModelSerializer):
    class Meta:
        model = ChannelCatesModel
        fields="__all__"

class ChannelGoodsSer(serializers.ModelSerializer):
    class Meta:
        model = ChannelGoodsMdoel
        fields="__all__"

class LessonSer(serializers.ModelSerializer):
    class Meta:
        model = LessonModel
        fields="__all__"


class ChapterSer(serializers.ModelSerializer):
    # chapterD=serializers.SerializerMethodField()
    # def get_chapterD(self,obj):
    #     return LessonSer(obj.chapter).data
    class Meta:
        model = ChapterModel
        fields="__all__"


class CuserSer(serializers.ModelSerializer):
    class Meta:
        model = CusersModel
        fields="__all__"

class CommentSer(serializers.ModelSerializer):
    useridd=CuserSer(source="userid")
    class Meta:
        model = CommentModel
        fields="__all__"

class ReplySer(serializers.ModelSerializer):
    class Meta:
        model = ReplyModel
        fields="__all__"

class SFser(serializers.ModelSerializer):
    userss=CuserSer(source="users",read_only=True)
    class Meta:
        model = SFModel
        fields="__all__"

class ShopCarSer(serializers.ModelSerializer):
    user=CuserSer(source="useridd",read_only=True)
    course=CourseSerilize(source="courseidd",read_only=True)
    class Meta:
        model = ShopCarModel
        fields="__all__"


class OrdersSer(serializers.ModelSerializer):
    # userid=serializers.PrimaryKeyRelatedField(queryset=CusersModel.objects.all(),source="ordersmodel_set",read_only=True)
    sourceinfo=serializers.SerializerMethodField()
    def get_sourceinfo(self,obj):

        details = obj.detailorder.all()
        serializer = OrderDetailSer(details, many=True)
        return serializer.data
    class Meta:
        model = OrdersModel
        fields="__all__"
        # fields = ['orderno']
class CouponSer(serializers.ModelSerializer):
    class Meta:
        model = CouponModel
        fields="__all__"

class UserCouponSer(serializers.ModelSerializer):
    #这个是一对一关系，为啥  TypeError: 'CouponModel' object is not iterable

    couponn=CouponSer(source="coupon")
    class Meta:
        model = UserCouponModel
        fields="__all__"

class OrderDetailSer(serializers.ModelSerializer):
    user=CuserSer()
    course=CourseSerilize()
    # user = serializers.PrimaryKeyRelatedField(queryset=CusersModel.objects.all(), read_only=True)
    class Meta:
        model = OrdersDetailModel
        fields="__all__"

class UserCourserCannalSer(serializers.ModelSerializer):
    class Meta:
        model = UserCourserCannal
        fields="__all__"



class UserCourseSer(serializers.ModelSerializer):
    courseid=CourseSerilize(read_only=True)
    class Meta:
        model = UserCourseModel
        fields = "__all__"

class TestPhoneSer(serializers.ModelSerializer):
    class Meta:
        model = TestModel
        fields = "__all__"